﻿/*
 * User: Zachary Gramana
 * Date: 4/14/2011
 * Time: 12:01 PM
 * 
 */
using System;
using System.ComponentModel;
using System.Security;

namespace SharpScript
{
    /// <summary>
    /// Description of SandboxSettings.
    /// </summary>
    [Serializable]
    public class SandboxInfo: IComparable, INotifyPropertyChanged
    {
        private PermissionSet _permissionsGranted;
        private String _scriptRootPath;

        /// <summary>
        /// Designates which permissions the Script Domain will be granted.
        /// </summary>
        public PermissionSet PermissionsGranted
        {
            get 
            {
                return _permissionsGranted;
            }
            set
            {
                if (_permissionsGranted == value) return;
                _permissionsGranted = value;
                OnInvokeScriptDomainConfigurationChanged("PermissionsGranted");
                OnPropertyChanged("PermissionsGranted");
            }
        }
        /// <summary>
        /// Indicates a string path to the AppDomainBase of the Script Domain.
        /// </summary>
        public String ScriptRootPath
        {
            get 
            {
                return _scriptRootPath;
            }
            set
            {
                if (_scriptRootPath == value) return;
                _scriptRootPath = value;
                OnInvokeScriptDomainConfigurationChanged("ScriptRootPath");
                OnPropertyChanged("ScriptRootPath");
            }
        }

        #region Implementation of IComparable

        /// <summary>
        /// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
        /// </summary>
        /// <returns>
        /// A value that indicates the relative order of the objects being compared. The return value has these meanings: Value Meaning Less than zero This instance is less than <paramref name="obj"/>. Zero This instance is equal to <paramref name="obj"/>. Greater than zero This instance is greater than <paramref name="obj"/>. 
        /// </returns>
        /// <param name="obj">An object to compare with this instance. </param><exception cref="T:System.ArgumentException"><paramref name="obj"/> is not the same type as this instance. </exception><filterpriority>2</filterpriority>
        public int CompareTo(object obj) 
        {
            var value = 0;
            var that = (SandboxInfo) obj;

            value += this.ScriptRootPath.CompareTo(that.ScriptRootPath);

            // If the PermissionsGranted are both initalized, then compare them.
            if (this.PermissionsGranted != null && that.PermissionsGranted != null)
            {
                // If the collections differ, we'll just use the collection size.
                // to decide the matter.
                if (!this.PermissionsGranted.Equals(that.PermissionsGranted))
                {
                    value += this.PermissionsGranted.Count - that.PermissionsGranted.Count;
                }
            }
            else // Otherwise, give "credit" to the object that *does* have contents.
            {
                if (this.PermissionsGranted != null && that.PermissionsGranted == null) value++;
                if (this.PermissionsGranted == null && that.PermissionsGranted != null) value--;
            }

            return value;
        }

        #endregion

        #region Implementation of INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

        public event EventHandler<AppDomainSettingsChangedHandlerArgs> ScriptDomainConfigurationChanged;
        private void OnInvokeScriptDomainConfigurationChanged(String message)
        {
            var handler = ScriptDomainConfigurationChanged;
            if (handler != null) handler(this, new AppDomainSettingsChangedHandlerArgs(message));
        }

    }
}
